package DBFunction;

import org.apache.log4j.Logger;
import DTO.Admin;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class AdminManager {
	public static Logger logger = Logger.getLogger("BuddyTrackingServer");
	public static ArrayList<Admin> getAdminList()throws SQLException{
		ArrayList<Admin> adminList = new ArrayList<Admin>();
		String sql ="SELECT * FROM buddytracking.admin";
		ResultSet result = DB.connectDB.ExecuteQuery(sql);
		while (result.next()) {
			Admin resultAdmin = new Admin();
			resultAdmin.setId(result.getString("AdminId"));
			resultAdmin.setAccount(result.getString("Account"));
			resultAdmin.setPasswords(result.getString("Passwords"));
			resultAdmin.setFirstName(result.getString("FirstName"));
			resultAdmin.setLastName(result.getString("LastName"));
			if(resultAdmin.getAccount().compareTo("admin")==0) continue;
			adminList.add(resultAdmin);
		}
		logger.info("AdminManager.getAdminList: Total is "+adminList.size());
		return adminList;
	}
	public static void insert(Admin admin)throws SQLException{
		String sql ="INSERT into buddytracking.admin (Account,Passwords,FirstName,LastName) VALUES ('"
			+admin.getAccount()+"','"+admin.getPasswords()+"','"+admin.getFirstName()+"','"
			+admin.getLastName()+"')" ;
			DB.connectDB.Update(sql);
			logger.info("AdminManager.insert: "+admin.getFirstName()+" " +admin.getLastName()+" inserted.");
	}
	public static void deleteById(String Id) throws SQLException {
		String sql ="DELETE FROM buddytracking.admin WHERE AdminId='"+Id+"'" ;
		DB.connectDB.Update(sql);
		logger.info("AdminManager.deleteById: Admin with Id "+Id+" deleted.");
	}
	public static Admin getAdminByAccount(String inputAccount)throws SQLException{
		Admin resultAdmin = new Admin();
		String sql ="SELECT * FROM buddytracking.admin WHERE Account='"+inputAccount+"'";
		ResultSet result = DB.connectDB.ExecuteQuery(sql);
		while (result.next()) {
			resultAdmin.setId(result.getString("AdminId"));
			resultAdmin.setAccount(result.getString("Account"));
			resultAdmin.setPasswords(result.getString("Passwords"));
			resultAdmin.setFirstName(result.getString("FirstName"));
			resultAdmin.setLastName(result.getString("LastName"));
		}
		logger.info("AdminManager.getAdminByAccount: "+resultAdmin.getAccount());
		return resultAdmin;
	}
	public static String adminAuthenicate(String inputAccount,String inputPasswords)throws SQLException{
		Admin resultAdmin = new Admin();
		String resultMessage = "default";
		String sql ="SELECT * FROM buddytracking.admin WHERE Account='"+inputAccount+"'";
		ResultSet result = DB.connectDB.ExecuteQuery(sql);
		while (result.next()) {
			resultAdmin.setId(result.getString("AdminId"));
			resultAdmin.setAccount(result.getString("Account"));
			resultAdmin.setPasswords(result.getString("Passwords"));
			resultAdmin.setFirstName(result.getString("FirstName"));
			resultAdmin.setLastName(result.getString("LastName"));
		}
		if(resultAdmin.getAccount().isEmpty()){
			logger.info("AdminManager.adminAuthenticate: Account "+inputAccount+" not found");
			resultMessage="wrong username";
			return resultMessage;
			}
		else if(resultAdmin.getPasswords().compareTo(inputPasswords)==0){
			resultMessage = "success";
			return resultMessage;
			}
		else {
			logger.info("AdminManager.adminAuthenticate: Wrong Passwords");
			resultMessage = "wrong passwords";
		}
		return resultMessage;
	}
}
